import { 
  queryChannel, 
  addChannel, 
  queryDetails, 
  changeChannelState, 
  changeChannelDetails, 
  changeChannelActivation,
  getBranchTreeList,
  activeShow
} from '../services/api';
import { message } from 'antd';

export default {
  namespace: 'channel',

  state: {
    channelList: [],//渠道列表
    page: [],
    loading: false,
    confirmLoading: false,
    branchList: [],//所属单位列表
    channel: [],//渠道详情信息
    branchTreeList: null,
    superiorBranchList: [],
  },

  effects: {

    *fetch({ payload ,callback}, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          loading: true,
        },
      }); 

      const response = yield call(queryChannel, payload);
      yield put({
        type: 'save',
        payload: {
          loading: false,
        },
      }); 
      if(!response.data){
        message.error(response.result.message)
      }else{
        yield put({
          type: 'save',
          payload: {
            channelList: response.data.list,
            page: response.data.page,
          },
        });
      }
      
      if (callback) callback(response);
    },

    *add({ payload, callback }, { call, put }) {
      console.log("add  payload", payload)
      yield put({
        type: 'save',
        payload: { confirmLoading: true },
      });

      const response = yield call(addChannel, payload);
      yield put({
        type: 'save',
        payload: { confirmLoading: false },
      });

      if (response.result.code == "200") {
        message.success('提交成功');
      } else {
        message.error(response.result.message);
      }
      yield put({
        type: 'save',
        payload: {
          data: response
        },
      });
      if (callback) callback();

    },

    *changeChannelState({ payload, callback }, { call, put }) {

      const response = yield call(changeChannelState, payload);

      if (response.result.code == "200") {
        message.success('改变状态成功');
      } else {
        message.error(response.result.message);
      }
      yield put({
        type: 'save',
        payload: { data: response },
      });
      if (callback) callback();

    },

    //显示渠道详情
    *details({ payload, callback }, { call, put }) {      
      yield put({
        type: 'save',
        payload: {
          loading: true,
        },
      });
      const response = yield call(queryDetails, payload);
      // console.log("response", response)
      yield put({
        type: 'save',
        payload: {
          branchList: response.data.branchList,
          channel: response.data.channel,
          loading: false,
        },
      });
     if(callback)callback()
    },
    //修改渠道详情
    *changeChannelDetails({ payload ,callback}, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          loading: true,
        },
      });
      const response = yield call(changeChannelDetails, payload);   
      if(response.result.code == 200){
        message.success("修改成功")
      }else{
        message.error(response.result.message)
      }
      yield put({
        type: 'save',
        payload: {
          loading: false,
        },
      });
      if (callback) callback();
    },
    //激活渠道
    *changeChannelActivation({ payload ,callback}, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          loading: true,
        },
      });
      const response = yield call(changeChannelActivation, payload);
      yield put({
        type: 'save',
        payload: {
          channel: response.data.channel,
          loading: false,
        },
      });
      if(callback)callback(response.result.code)
    },

    *getBranchTreeList({ payload ,callback}, { call, put }) {
      const response = yield call(getBranchTreeList, payload);
      if(response.result.code == 200){
        yield put({
          type: 'save',
          payload: {
            branchTreeList: response.data,
          },      
        });
      }
      if(callback)callback();
    },
  },


  reducers: {//纯函数， 输入什么输出就是什么
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
